package clientGUI.studentGUI;
import javax.swing.*;

import clientApp.Plan;
import clientApp.Semester;

import java.awt.event.*;
import java.util.ArrayList;
import java.util.LinkedList;

public class RemoveCoursesGUI {
	private Plan	plan;
	private JFrame	frame;
	private LinkedList<String>	removeCourseList;
	// constructor
	public RemoveCoursesGUI(Plan plan)
	{
		this.plan = plan;
		this.removeCourseList = new LinkedList<String>();
		
		JPanel removePane = RemoveCoursePanel();
		this.frame = GUI_Function_Library.displayPane("Remove Courses from Plan", removePane, false);
	}

	// add functionality to button
	private JPanel RemoveCoursePanel()
	{	
		// 3 - list of classes
		final JPanel classListInteriorPane = new JPanel();
		
		// 1 - instruction
		JLabel instructionLabel = new JLabel("Please select courses to add to your schedule");
		// 2 - select semester
		ArrayList<String> semesterArrayList = plan.getAllSemesterSeasonSpaceYear();
		String[] semesters = new String[semesterArrayList.size()];
		for(int i = 0; i < semesterArrayList.size(); i++)
			semesters[i] = semesterArrayList.get(i);
		final JComboBox semesterDropdown = new JComboBox(semesters);
		semesterDropdown.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				// clear current list
				classListInteriorPane.removeAll();
				removeCourseList.clear();
				
				// repopulate with classes corresponding to the semester
					// extract semester
				String[] semesterArr = ((String) semesterDropdown.getSelectedItem()).split(" ");
				Semester semester = plan.getSemester(Integer.parseInt(semesterArr[0]), Integer.parseInt(semesterArr[1]));
					// extract classes
				for(final String courseID: semester.getCourseIDs())
				{
					// make a checkbox
					JCheckBox classCheckBox = new JCheckBox(courseID);
					classCheckBox.addItemListener(new ItemListener(){
						public void itemStateChanged(ItemEvent e)
						{
							switch(e.getStateChange())
							{
							case ItemEvent.SELECTED:
								removeCourseList.add(courseID);
								break;
							case ItemEvent.DESELECTED:
								removeCourseList.removeFirstOccurrence(courseID);
								break;
							}
						}
					});
					classListInteriorPane.add(classCheckBox);
				}
			}
		});
		// 3 - select classes
		JPanel classListExteriorPane = new JPanel();
		classListExteriorPane.add(classListInteriorPane);
		
		// 4 - button
		JButton removeSelectedButton = new JButton("Remove Selected");
		removeSelectedButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{
				// SUBMIT REQUEST TO Virtual Advisor
				
				GUI_Function_Library.undisplayFrame(frame);
			}
		});
		
		JPanel mainPane = new JPanel();
		mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.PAGE_AXIS));
		mainPane.add(instructionLabel);
		mainPane.add(semesterDropdown);
		mainPane.add(classListExteriorPane);
		mainPane.add(removeSelectedButton);
			
		return mainPane;
	}
}
